import axios from "axios";

import router from "../router";
const request = axios.create({
  baseURL: "/webApi", // 所有的请求地址前缀部分
  timeout: 5000, // 请求超时时间毫秒
  withCredentials: true, // 异步请求携带cookie
  headers: {
    "Content-Type": "application/json",
    "X-Requested-With": "XMLHttpRequest",
  },
});

request.interceptors.request.use((config: any) => {
  const token = localStorage.getItem("token");
  config.headers["token"] = token;
  return config;
});

request.interceptors.response.use(function (response) {
  if (response.data.code == 403) {
    router.push("/login");
  }

  return response;
});

export default request;
